home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / nmsmax < prev    next >
Text File  |  1994-09-23  |  7KB  |  228 lines

  1. #
  2. #NMSMAX  [x, fmax, nf] = NMSMAX(fun, x0, STOPIT, SAVIT) attempts to
  3. #        maximize the function specified by the string fun, using the
  4. #        starting vector x0.  The Nelder-Mead direct search method is used.
  5. #        Output arguments:
  6. #               x    = vector yielding largest function value found,
  7. #               fmax = function value at x,
  8. #               nf   = number of function evaluations.
  9. #        The iteration is terminated when either
  10. #               - the relative size of the simplex is <= STOPIT(1)
  11. #                 (default 1e-3),
  12. #               - STOPIT(2) function evaluations have been performed
  13. #                 (default inf, i.e., no limit), or
  14. #               - a function value equals or exceeds STOPIT(3)
  15. #                 (default inf, i.e., no test on function values).
  16. #        The form of the initial simplex is determined by STOPIT(4):
  17. #           STOPIT(4) = 0: regular simplex (sides of equal length, the default)
  18. #           STOPIT(4) = 1: right-angled simplex.
  19. #        Progress of the iteration is not shown if STOPIT(5) = 0 (default 1).
  20. #        If a non-empty fourth parameter string SAVIT is present, then
  21. #        `SAVE SAVIT x fmax nf' is executed after each inner iteration.
  22. #        NB: x0 can be a matrix.  In the output argument, in SAVIT saves,
  23. #            and in function calls, x has the same shape as x0.
  24.  
  25. # References:
  26. # J.E. Dennis, Jr., and D.J. Woods, Optimization on microcomputers:
  27. #     The Nelder-Mead simplex algorithm, in New Computing Environments:
  28. #     Microcomputers in Large-Scale Computing, A. Wouk, ed., Society for
  29. #     Industrial and Applied Mathematics, Philadelphia, 1987, pp. 116-122.
  30. #  N.J. Higham, Optimization by direct search in matrix computations,
  31. #     Numerical Analysis Report No. 197, University of Manchester, UK, 1991;
  32. #     to appear in SIAM J. Matrix Anal. Appl, 14 (2), April 1993.
  33.  
  34. # This is a heavily modified version of FMINS.M supplied with 386-MATLAB
  35. # version 3.5j.
  36.  
  37. # By Nick Higham, Department of Mathematics, University of Manchester, UK.
  38. #                 na.nhigham@na-net.ornl.gov
  39. # July 27, 1991.
  40.  
  41. # Translated to RLaB, Ian Searle
  42. # Feburary 1994.
  43.  
  44. nmsmax = function (fun, X, stopit, savit)
  45. {
  46.   global (eps)
  47.  
  48.   x = X;    # Copy the input
  49.   n = prod(size(x));
  50.   x0 = x[:];    # Work with column vector internally.
  51.  
  52.   # Set up convergence parameters etc.
  53.   if (!exist (stopit)) { stopit[1] = 1e-3; }
  54.   tol = stopit[1];    # Tolerance for cgce test based on relative size of simplex.
  55.   if (max(size(stopit)) == 1) { stopit[2] = inf(); }    # Max no. of f-evaluations.
  56.   if (max(size(stopit)) == 2) { stopit[3] = inf(); }    # Default target for f-values.
  57.   if (max(size(stopit)) == 3) { stopit[4] = 0; }    # Default initial simplex.
  58.   if (max(size(stopit)) == 4) { stopit[5] = 1; }    # Default: show progress.
  59.   trace  = stopit[5];
  60.   if (!exist (savit)) { savit = []; }            # File name for snapshots.
  61.  
  62.   V = [zeros(n,1), eye(n,n)];
  63.   f = zeros(n+1,1);
  64.   V[;1] = x0; 
  65.   x = reshape (x0, x.nr, x.nc); 
  66.   f[1] = fun (x);
  67.   fmax_old = f[1];
  68.  
  69.   if (trace) { printf("f(x0) = %9.4e\n", f[1]); }
  70.  
  71.   k = 0; m = 0;
  72.  
  73.   # Set up initial simplex.
  74.   scale = max([norm(x0,"i"),1]);
  75.   if (stopit[4] == 0)
  76.   {
  77.     # Regular simplex - all edges have same length.
  78.     # Generated from construction given in reference [18, pp. 80-81] of [1].
  79.     alpha = scale / (n*sqrt(2)) * [ sqrt(n+1)-1+n, sqrt(n+1)-1 ];
  80.     V[;2:n+1] = (x0 + alpha[2]*ones(n,1)) * ones(1,n);
  81.     for (j in 2:n+1)
  82.     {
  83.       V[j-1;j] = x0[j-1] + alpha[1];
  84.       x = reshape (V[;j], x.nr, x.nc); 
  85.       f[j] = fun (x);
  86.     }
  87.   else
  88.     # Right-angled simplex based on co-ordinate axes.
  89.     alpha = scale*ones(n+1,1);
  90.     for (j in 2:n+1)
  91.     {
  92.       V[;j] = x0 + alpha[j]*V[;j];
  93.       x = reshape (V[;j], x.nr, x.nc); 
  94.       f[j] = fun (x);
  95.     }
  96.   }
  97.   nf = n+1;
  98.   how = "initial  ";
  99.  
  100.   temp = sort (f).val;
  101.   j = sort (f).ind;
  102.   j = j[n+1:1:-1];
  103.   f = f[j]; 
  104.   V = V[;j];
  105.  
  106.   alpha = 1;  beta = 1/2;  gamma = 2;
  107.  
  108.   while (1)    ###### Outer (and only) loop.
  109.   {
  110.     k = k+1;
  111.  
  112.     fmax = f[1];
  113.     if (fmax > fmax_old)
  114.     {
  115.       if (!isempty(savit))
  116.       {
  117.         x = reshape (V[;1], x.nr, x.nc); 
  118.         write("savit", x, fmax, nf);
  119.       }
  120.       if (trace)
  121.       {
  122.         printf("Iter. %2.0f"', k);
  123.         printf("  how = %s  ", how);
  124.         printf("nf = %3.0f,  f = %9.4e  (%2.1f)\n", nf, fmax, ...
  125.                100*(fmax-fmax_old)/(abs(fmax_old)+eps));
  126.       }
  127.     }
  128.     fmax_old = fmax;
  129.  
  130.     ### Three stopping tests from MDSMAX.M
  131.  
  132.     # Stopping Test 1 - f reached target value?
  133.     if (fmax >= stopit[3])
  134.     {
  135.       msg = "Exceeded target...quitting\n";
  136.       break  # Quit.
  137.     }
  138.  
  139.     # Stopping Test 2 - too many f-evals?
  140.     if (nf >= stopit[3])
  141.     {
  142.       msg = "Max no. of function evaluations exceeded...quitting\n";
  143.       break  # Quit.
  144.     }
  145.  
  146.     # Stopping Test 3 - converged?   This is test (4.3) in [1].
  147.     v1 = V[;1];
  148.     size_simplex = norm(V[;2:n+1]-v1[;ones(1,n)],"1") / max([1, norm(v1,"1")]);
  149.     if (size_simplex <= tol)
  150.     {
  151.       sprintf(msg, "Simplex size %9.4e <= %9.4e...quitting\n", ...
  152.               size_simplex, tol);
  153.       break  # Quit.
  154.     }
  155.  
  156.     #  One step of the Nelder-Mead simplex algorithm
  157.     #  NJH: Altered function calls and changed CNT to NF.
  158.     #       Changed each `fr < f(1)' type test to `>' for maximization
  159.     #       and re-ordered function values after sort.
  160.  
  161.     vbar = (sum(V[;1:n]')/n)';    # Mean value
  162.     vr = (1 + alpha)*vbar - alpha*V[;n+1]; 
  163.     x = reshape (vr, x.nr, x.nc);
  164.     fr = fun (x);
  165.     nf = nf + 1;
  166.     vk = vr;  fk = fr; how = "reflect, ";
  167.     if (fr > f[n])
  168.     {
  169.       if (fr > f[1])
  170.       {
  171.         ve = gamma*vr + (1-gamma)*vbar; 
  172.         x = reshape (ve, x.nr, x.nc);
  173.         fe = fun (x);
  174.         nf = nf + 1;
  175.         if (fe > f[1])
  176.         {
  177.           vk = ve; fk = fe;
  178.           how = "expand,  ";
  179.         }
  180.       }
  181.     else
  182.       vt = V[;n+1];
  183.       ft = f[n+1];
  184.       if (fr > ft)
  185.       {
  186.         vt = vr;
  187.         ft = fr;
  188.       }
  189.       vc = beta*vt + (1-beta)*vbar; 
  190.       x = reshape (vc, x.nr, x.nc);
  191.       fc = fun (x);
  192.       nf = nf + 1;
  193.       if (fc > f[n])
  194.       {
  195.         vk = vc; fk = fc;
  196.         how = "contract,";
  197.       else
  198.         for (j in 2:n)
  199.         {
  200.           V[;j] = (V[;1] + V[;j])/2;
  201.           x = reshape (V[;j], x.nr, x.nc);
  202.           f[j] = fun (x);
  203.         }
  204.         nf = nf + n-1;
  205.         vk = (V[;1] + V[;n+1])/2; 
  206.         x = reshape (vk, x.nr, x.nc);
  207.         fk = fun (x);
  208.         nf = nf + 1;
  209.         how = "shrink,  ";
  210.       }
  211.     }
  212.     V[;n+1] = vk;
  213.     f[n+1] = fk;
  214.     temp = sort(f).val;
  215.     j = sort (f).ind;
  216.     j = j[n+1:1:-1];
  217.     f = f[j]; 
  218.     V = V[;j];
  219.  
  220.   }    ###### End of outer (and only) loop.
  221.  
  222.   # Finished.
  223.   if (trace) { printf(msg); }
  224.   x = reshape (V[;1], x.nr, x.nc);
  225.  
  226.   return <<x = x; fmax = fmax; nf = nf>>;
  227. };
  228.